package cn.wolfcode.car.business.web.controller.appointment;
import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.AppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.util.StringUtils;
import cn.wolfcode.car.common.web.AjaxResult;
import net.sf.jsqlparser.schema.Table;
import org.apache.poi.util.StringUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author WaveHi
 * @Date 14/11/2021 上午 11:05
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
@Controller
@RequestMapping("/business/appointment")
public class AppointmentController {

    private static final String prefix = "business/appointment/";

    @Autowired
    private AppointmentService appointmentService;

    @RequiresPermissions("business:appointment:view")
    @RequestMapping("/listPage")
    public String listPage() {
        return prefix + "list";
    }

    @RequiresPermissions("business:appointment:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<Appointment> query(
            @RequestParam(value = "pageNo", required = false) Integer pageNo,
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
            AppointmentQuery appointmentQuery) {
        //Judge
        pageNo = pageNo != null ? pageNo : 1;
        pageSize = pageSize != null ? pageSize : 3;
        boolean cusEmpty = StringUtils.isEmpty(appointmentQuery.getCustomerName());
        boolean phoneEmpty = StringUtils.isEmpty(appointmentQuery.getCustomerPhone());
        boolean statusEmpty = !(appointmentQuery.getStatus() != null && appointmentQuery.getStatus() >= 0 && appointmentQuery.getStatus() <= 4);
        if(cusEmpty) {
            appointmentQuery.setCustomerName(null);
        }else {
            appointmentQuery.setCustomerName(appointmentQuery.getCustomerName().trim());
        }
        if(phoneEmpty) {
            appointmentQuery.setCustomerPhone(null);
        }else {
            appointmentQuery.setCustomerPhone(appointmentQuery.getCustomerPhone().trim());
        }
        if (statusEmpty) {
            appointmentQuery.setStatus(null);
        }
        //Query
        return appointmentService.getPartOfAppointments(appointmentQuery);
    }

    @RequiresPermissions("business:appointment:add")
    @RequestMapping("/addPage")
    public String addPage() {
        return prefix + "add";
    }

    @RequiresPermissions("business:appointment:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult add(Appointment appointment){
        //Judge data
        boolean cusEmpty = StringUtils.isEmpty(appointment.getCustomerName());
        boolean phoneEmpty = StringUtils.isEmpty(appointment.getCustomerPhone());
        boolean licEmpty = StringUtils.isEmpty(appointment.getLicensePlate());
        boolean carSEmpty = StringUtils.isEmpty(appointment.getCarSeries());
        boolean stEmpty =
                !(appointment.getServiceType() != null
                        && appointment.getServiceType() >= 0
                        && appointment.getServiceType() <= 1);
        boolean infoEmpty = StringUtils.isEmpty(appointment.getInfo());
        boolean statusEmpty =
                !(appointment.getStatus() != null
                        && appointment.getStatus() >= 0
                        && appointment.getStatus() <= 4);
        if(!cusEmpty && !phoneEmpty && !licEmpty && !carSEmpty) {
            if (!cusEmpty) {
                appointment.setCustomerName(appointment.getCustomerName().trim());
            }
            if (!phoneEmpty) {
                appointment.setCustomerPhone(appointment.getCustomerPhone().trim());
            }
            if (!licEmpty) {
                appointment.setLicensePlate(appointment.getLicensePlate().trim());
            }
            if (!carSEmpty) {
                appointment.setCarSeries(appointment.getCarSeries().trim());
            }
            if (stEmpty) {
                appointment.setServiceType(0);
                ;
            }
            if (!infoEmpty) {
                appointment.setInfo(appointment.getInfo().trim());
            }
            if (statusEmpty) {
                appointment.setStatus(0);
            }
            appointmentService.add(appointment);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @RequiresPermissions("business:appointment:edit")
    @RequestMapping("/editPage")
    public String editPage(Model model, Long id){
        if(id != null) {
            model.addAttribute("appointment", appointmentService.getAppointment(id));
        }
        return prefix + "edit";
    }

    @RequiresPermissions("business:appointment:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(Appointment appointment) {
        //Judge data
        boolean cusEmpty = StringUtils.isEmpty(appointment.getCustomerName());
        boolean phoneEmpty = StringUtils.isEmpty(appointment.getCustomerPhone());
        boolean licEmpty = StringUtils.isEmpty(appointment.getLicensePlate());
        boolean carSEmpty = StringUtils.isEmpty(appointment.getCarSeries());
        boolean stEmpty =
                !(appointment.getServiceType() != null
                        && appointment.getServiceType() >= 0
                        && appointment.getServiceType() <= 1);
        boolean infoEmpty = StringUtils.isEmpty(appointment.getInfo());
        boolean statusEmpty =
                !(appointment.getStatus() != null
                        && appointment.getStatus() >= 0
                        && appointment.getStatus() <= 4);
        if(!cusEmpty && !phoneEmpty && !licEmpty && !carSEmpty) {
            if (!cusEmpty) {
                appointment.setCustomerName(appointment.getCustomerName().trim());
            }
            if (!phoneEmpty) {
                appointment.setCustomerPhone(appointment.getCustomerPhone().trim());
            }
            if (!licEmpty) {
                appointment.setLicensePlate(appointment.getLicensePlate().trim());
            }
            if (!carSEmpty) {
                appointment.setCarSeries(appointment.getCarSeries().trim());
            }
            if (stEmpty) {
                appointment.setServiceType(0);
                ;
            }
            if (!infoEmpty) {
                appointment.setInfo(appointment.getInfo().trim());
            }
            if (statusEmpty) {
                appointment.setStatus(0);
            }
            appointmentService.update(appointment);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @RequiresPermissions("business:appointment:cancel")
    @RequestMapping("/cancel")
    @ResponseBody
    public AjaxResult cancel(@RequestParam("id") Long id) {
        if(id != null) {
            appointmentService.cancel(id);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @RequiresPermissions("business:appointment:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(@RequestParam(value = "ids", required = false) Long[] ids) {
        if(ids != null && ids.length > 0) {
            appointmentService.deleteBatch(ids);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @RequiresPermissions("business:appointment:arrival")
    @RequestMapping("/arrival")
    @ResponseBody
    public AjaxResult arrival(Long id) {
        if(id != null) {
            appointmentService.arrival(id);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @RequiresPermissions("business:carMaintenanceInfo:generateStatement")
    @RequestMapping("/generateStatement")
    @ResponseBody
    public AjaxResult generateStatement(Long appointmentId) {
        Long statementId = appointmentService.generateStatement(appointmentId);
        return AjaxResult.success(statementId);
    }
}
